﻿app.controller('UserController', function ($scope, $rootScope, $compile, userFactory, groupFactory, notificationFactory) {

    init();

    //all implement data model call in a init() function
    function init() {
        $scope.routeId = 'userManage';
        $scope.fileSever = mainFileAddress;
        $scope.roleByGroup = JSON.parse(localStorage.getItem("menu"));
        $scope.userName = $rootScope.userName;
        //count--------------
        notificationFactory.countAddFriend(localStorage.getItem("idkey")).success(function (data) {
            $scope.countAddFriend = data;
        });
        notificationFactory.countFromRoom(localStorage.getItem("idkey")).success(function (data) {
            $scope.countFromRoom = data;
        });
        notificationFactory.countNewMessage(localStorage.getItem("idkey")).success(function (data) {
            $scope.countNewMessage = data;
        });
        
        var dataPost = {
            PageIndex: 1,
            PageSize: 6
        };
        // tab-------------------------------------
        $scope.createUserPanel = false;
        $scope.userPanel = true;
        $scope.createUserPanelClick = function () {
            $scope.createUserPanel = true;
            $scope.userPanel = false;
        };
        $scope.userPanelClick = function () {
            $scope.createUserPanel = false;
            $scope.userPanel = true;
        };
        //use manager tab-----------------------------------------
        $scope.groups = {};
        groupFactory.getGroups().success(function (data) {
            $scope.groups = data;
        });
        $scope.users = {};
        $scope.hasNextPage = {};
        $scope.hasPreviousPage = {};
        $scope.totalPages = {};
        var newp;
        var dom;
        userFactory.getUsers(dataPost).success(function (data) {
            $scope.users = data.List;
            $scope.hasNextPage = data.HasNextPage;
            $scope.hasPreviousPage = data.HasPreviousPage;
            $scope.totalPages = data.TotalPages;
            dom = $('#paging');
            dom.append('<span class="current-page">' + dataPost.PageIndex + '</span>');
            if ($scope.hasNextPage) {
                newp = dataPost.PageIndex + 1;
                dom.append('<span class="next-page" ng-click=paging(' + newp + ')>»</span>');
            }
            if (newp != $scope.totalPages)
                dom.append('<span class="last-page" ng-click=paging(' + $scope.totalPages + ')>Trang cuối</span>');
            var compiled = $compile(dom);
            compiled($scope);
        });


        $scope.paging = function (newp) {
            dataPost.PageIndex = newp;
            dom = $('#paging');
            dom.empty();
            var g = $("#groupList").find('option:selected').val();
            if ($("#groupList").find('option:selected').val() === "0") {
                userFactory.getUsers(dataPost).success(function (data) {
                    $scope.users = data.List;
                    $scope.hasNextPage = data.HasNextPage;
                    $scope.hasPreviousPage = data.HasPreviousPage;
                    appendPage();
                });
            }

            else {
                dataPost.InputId = $("#groupList").find('option:selected').val();
                userFactory.getByGroup(dataPost).success(function (data) {
                    $scope.users = data.List;
                    $scope.hasNextPage = data.HasNextPage;
                    $scope.hasPreviousPage = data.HasPreviousPage;
                    appendPage();
                });
            }
            function appendPage() {
                if (newp != 1)
                    dom.append('<span class="first-page" ng-click=paging(1)>«</span>');
                if ($scope.hasPreviousPage) {
                    dom.append('<span class="prev-page" ng-click=paging(' + (dataPost.PageIndex - 1) + ')>‹</span>');

                }
                dom.append('<span class="current-page">' + dataPost.PageIndex + '</span>');
                if ($scope.hasNextPage) {
                    dom.append('<span class="next-page" ng-click=paging(' + (dataPost.PageIndex + 1) + ')>›</span>');
                }
                if (newp != $scope.totalPages)
                    dom.append('<span class="last-page" ng-click=paging(' + $scope.totalPages + ')>»</span>');

                var compiled = $compile(dom);
                compiled($scope);

            }

        };
        // xu ly du lieu
        $scope.loadUsersByGroup = function (id) {
            dataPost = {
                PageIndex: 1,
                PageSize: 6,
                InputId: id
            };
            if (id != 0) {
                userFactory.getByGroup(dataPost).success(function (data) {
                    $scope.users = data.List;
                    $scope.hasNextPage = data.HasNextPage;
                    $scope.hasPreviousPage = data.HasPreviousPage;
                    $scope.totalPages = data.TotalPages;
                    firstPageAppend();
                });
            } else {
                userFactory.getUsers(dataPost).success(function (data) {
                    $scope.users = data.List;
                    $scope.hasNextPage = data.HasNextPage;
                    $scope.hasPreviousPage = data.HasPreviousPage;
                    $scope.totalPages = data.TotalPages;
                    firstPageAppend();
                });
            }
        }
        function firstPageAppend() {
            dom = $('#paging');
            dom.html("");
            dom.append('<span class="current-page">' + dataPost.PageIndex + '</span>');
            if ($scope.hasNextPage) {
                newp = dataPost.PageIndex + 1;
                dom.append('<span class="next-page" ng-click=paging(' + newp + ')>›</span>');
            }
            if (newp != $scope.totalPages)
                dom.append('<span class="last-page" ng-click=paging(' + $scope.totalPages + ')>»</span>');
            var compiled = $compile(dom);
            compiled($scope);
        }
        $scope.searchUs = function () {
            var x = $('#inputSearch').val();
            dataPost = {
                PageIndex: 1,
                PageSize: 6,
                SearchString: x
            };
            userFactory.searchUser(dataPost).success(function (data) {
                $scope.users = data.List;
                $scope.hasNextPage = data.HasNextPage;
                $scope.hasPreviousPage = data.HasPreviousPage;
                $scope.totalPages = data.TotalPages;
                firstPageAppend();
            });
        }
        $scope.activeUser = function (id) {
            if (id != localStorage.getItem("idkey")) {
                userFactory.blockOrActive(id).success(function() {
                    //var user = $('#user_' + id);
                    var temp = $scope.users;
                    for (var t = 0; t < temp.length; t++) {
                        if (temp[t].Id === id)
                            temp[t].Block = !temp[t].Block;
                    }
                    $scope.users = {};
                    $scope.$watchCollection($scope.users, function() {
                        $scope.users = temp;
                    });
                });
            }
        };
        $scope.deleteUser = function (id) {
            if (id != localStorage.getItem("idkey")) {
                var result = confirm("You want to delete?");
                if (result == true) {
                    userFactory.delUser(id);
                    $("#tr_user_" + id).fadeOut(500);
                }
            }

        }
        //create user tab-------------------------------- 
        $scope.formData = { UserName: null, Password: null, PasswordC: null, FullName: null, Email: null, PhoneNumber: null };
        $scope.checkEmail = true;
        $scope.checkEmailOnblur = function () {
            var emailstring = { SearchString: $scope.formData.Email };
            userFactory.checkEmail(emailstring).success(function (data) {
                if (data == 'true')
                    $scope.checkEmail = true;
                else {
                    $scope.checkEmail = false;
                }
            });
        };
        $scope.checkEmailFocus = function () {
            $scope.checkEmail = true;
        };


        $scope.checkUser = true;
        $scope.checkUserOnblur = function () {
            var userstring = { SearchString: $scope.formData.UserName };
            userFactory.checkUser(userstring).success(function (data) {
                if (data == 'true')
                    $scope.checkUser = true;
                else {
                    $scope.checkUser = false;
                }
            });
        };
        $scope.checkUserFocus = function () {
            $scope.checkUser = true;
        };
        $scope.createUser = function () {
            if ($scope.checkUser && $scope.checkEmail) {
                userFactory.postUser($scope.formData);
            }
        };
        $scope.clearText= function() {
            $scope.formData = { UserName: null, Password: null, PasswordC: null, FullName: null, Email: null, PhoneNumber: null };
        }
        //-----------------------------------------------
        createMenu();
        $("tr:nth-child(even)").addClass("even");
    }
});